﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Zor_API
{
    /// <summary>
    /// 获取房屋信息
    /// </summary>
    public class PropertyInfo
    {
        #region 远程访问方法
        /// <summary>
        /// API路径
        /// </summary>
        //public static string apiPath = "http://localhost:20100/";
        public static string apiPath = "http://192.168.0.173:8011/";
        // public static string apiPath = "http://117.22.254.106:8088/";
        /// <summary>
        /// POST远程数据
        /// </summary>
        /// <param name="Url">远程地址</param>
        /// <param name="postDataStr">提交数据</param>
        /// <returns></returns>
        private static string HttpPost(string url, IDictionary<string, string> para)
        {
           
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);//这是你要提交的url
            Encoding encoding = Encoding.UTF8;


            StringBuilder buffer = new StringBuilder();//这是要提交的数据
              int i = 0;

            //通过泛型集合转成要提交的参数和数据
              foreach (string key in para.Keys)
            {
                if (i > 0)
                {
                    buffer.AppendFormat("&{0}={1}", key, para[key]);
                }
                else
                {
                    buffer.AppendFormat("{0}={1}", key, para[key]);
                }
                i++;
            } 

            //通过泛型转化得到的提交数据：LinkTel=第二次测试&CarBrand=1111&Loads=101&UserId=50&SortId=1
            //其实与上面的直接拼接参数无异，
              byte[] bs = Encoding.UTF8.GetBytes(buffer.ToString());//UTF-8

            string responseData = String.Empty;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            req.ContentLength = bs.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bs, 0, bs.Length);
                reqStream.Close();
            }
            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                {
                    responseData = reader.ReadToEnd().ToString();
                }
                return responseData;                
            }
        }
        /// <summary>
        /// Get远程数据
        /// </summary>
        /// <param name="Url">远程地址</param>
        /// <param name="postDataStr">提交数据</param>
        /// <returns></returns>
        public static string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        #endregion

        /// <summary>
        /// 获取指定街办所有房屋信息
        /// </summary>
        /// <param name="Dt_ParentId">街办ID</param>
        /// <returns></returns>        
        public static string GetPropertyInfoList(Dictionary<string, string> pi)
        {
            string strUrl = apiPath + "api/PropertyInfo/GetPropertyInfoList";
            string str = HttpPost(strUrl, pi);
            return str;
        }
        /// <summary>
        /// 获取所有诚信信息
        /// </summary>
        /// <returns></returns>
        public static string GetSincerityInfoList(Dictionary<string, string> pi)
        {
            string strUrl = apiPath + "api/SincerityInfo/GetSincerityInfoList";
            string str = HttpPost(strUrl, pi);
            return str;
        }
        /// <summary>
        /// 获取统计信息
        /// </summary>
        /// <param name="Dt_ParentId"></param>
        /// <param name="Dt_Id"></param>
        /// <returns></returns>
        public static string GetPropertyInfoStatisticsByYear(Dictionary<string,string> si)
        {
            // public static List<StatisticalResults> GetPropertyInfoStatisticsByTimeSlot(DateTime startTime, DateTime endTime)

            string strUrl = apiPath + "api/PropertyInfo/GetPropertyInfoStatisticsByTimeSlot";
            string str = HttpPost(strUrl, si);
            return str;
        }

    }
}
